/*
 * @Author: yangfan 1437446666@qq.com
 * @Date: 2023-02-27 11:49:28
 * @LastEditors: yangfan 1437446666@qq.com
 * @LastEditTime: 2023-03-10 09:18:02
 * @FilePath: \s_2006A_shixun\src\store\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStore } from 'vuex'
//登录
import { HeadLogin, Authentication } from '../api/loginPage'
//存取Cokkie
import Cookie from 'js-cookie'
// vuex持久化
import createPersistedState from 'vuex-persistedstate'
//组件库的message
import { message } from 'ant-design-vue'
//路由表
import RouterTypr from '../router/userRouter'
//过滤路由表的方法
import { filterRouter } from '@/utils/filterRouter'
//路由的实例
import router from '../router/index'
export default createStore({
  state: {
    //存储用户信息
    user: {},
    //存储路由表
    Routers: []
  },
  getters: {},
  mutations: {
    //设置state
    set_state(state, data: any) {
      // console.log(data)
      state[data.key] = data.data
    }
    //储存路由表
    // SET_Render_Router(state, data: any) {
    //   // console.log(state.renderRouter)
    //   // console.log(data)
    //   state.renderRouter = data
    // }
  },
  actions: {
    //登录
    async Login({ commit, dispatch }, payload) {
      //登录
      const res = await HeadLogin(payload)
      console.log(res)

      if (res.status === 200) {
        //成功提示
        message.success('登录成功')
        //储存token
        Cookie.set('token', res.data.data.token)
        //跳转home页

        //获取动态路由表
        dispatch('getRouters', 'visitor')
        // 通过角色获取动态路由
        filterRouter(RouterTypr, 'visitor').forEach((item) => {
          router.addRoute(item)
        })
        router.push('/home')
        const getRouters = router.getRoutes()
        sessionStorage.setItem('routerTable', JSON.stringify(getRouters))
        // console.log(resRouter)
        // console.log(renderRouter)
        // commit('SET_Render_Router', getRouters.splice(0, 12))
        // Cookie.set('renderRouter', resRouter)
        // 存储用户信息
        commit('set_state', {
          key: 'routerTable',
          data: JSON.stringify(getRouters)
        })
        commit('set_state', { key: 'user', data: res.data.data })
        dispatch('authentication', res.data.data)
      } else {
        //失败提示
        message.error(res.data.message)
      }
    },
    //获取动态路由表
    async getRouters({ commit }, auth) {
      console.log(auth)
      //筛选路由表
      const res = filterRouter(RouterTypr, auth)
      //添加路由
      res.forEach((item) => {
        // console.log(item)
        router.addRoute(item)
      })

      //跳转home页
      router.push('/home')
      //存储路由表
      commit('set_state', { key: 'Routers', data: res })
    },
    //身份的认证
    async authentication({ commit }, auth) {
      const res = await Authentication(auth)
      console.log(res)
    }
  },
  modules: {},
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
      reducer(state: { user: any }) {
        return {
          user: state.user
        }
      }
    })
  ]
})
